package com.wuzuqing.android.eightdiagrams

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.RadioGroup
import android.widget.Spinner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaModel
import com.wuzuqing.android.eightdiagrams.utils.DataUtils
import java.util.Calendar


class Fucai3DOpenActivity : BaseActivity() {

    lateinit var dunSpinner: Spinner
    lateinit var rcv: RecyclerView
    lateinit var cb3d: CheckBox
    lateinit var rgType: RadioGroup
    private val mAdapter by lazy {
        Fucai3DOpenAdapter()
    }
    val itemList = mutableListOf<String>()
    private var mSelectJuIndex = 0
    private var mSelectTime = 0L
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fucai3d_open)
        rcv = findViewById(R.id.rcv)
        cb3d = findViewById(R.id.cb_3d)
        rgType = findViewById(R.id.rg_check)
        dunSpinner = findViewById(R.id.dunSpinner)

        itemList.add("无")
        for (i in 1 until 10) {
            itemList.add("阳$i")
        }
        for (i in 1 until 10) {
            itemList.add("阴$i")
        }

        dunSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                mSelectJuIndex = position
                mAdapter.setJu(itemList[mSelectJuIndex])
                loadData()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }
        dunSpinner.adapter = ArrayAdapter(
            this, android.R.layout.simple_spinner_item, android.R.id.text1, itemList
        )
        rgType.setOnCheckedChangeListener { _, checkedId ->
            val type = when (checkedId) {
                R.id.rb_gan -> 1
                R.id.rb_men -> 2
                R.id.rb_xing -> 3
                else -> 4
            }
        }
        rcv.layoutManager = LinearLayoutManager(this)
        rcv.adapter = mAdapter

        findViewById<View>(R.id.selectTime).setOnClickListener {
            CardDatePickerDialog.builder(this)
                .setChooseDateModel(DateTimeConfig.DATE_LUNAR)
                .setDefaultTime(mSelectTime)
                .setOnChoose { time ->
                    reloadData(time)
                }
                .build().show()
        }
    }

    private fun reloadData(time: Long) {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        DataUtils.saveHHmmss(calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE))
        loadData()
    }

    private fun loadData() {
        Thread {
            val models = DataUtils.getOpenDate(itemList[mSelectJuIndex])
            runOnUiThread {
                mAdapter.setList(models)
            }
        }.start()
    }
}
